package cn.com.czpb.client.module.news.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import cn.com.czpb.client.R;
import cn.com.czpb.client.app.ListBaseAdapter;
import cn.com.czpb.client.model.news.NewsTagBean;
import cn.com.czpb.client.utils.LogUtils;
import cn.com.czpb.client.utils.ViewHolder;

import java.util.HashMap;
import java.util.List;

public class NewsCancelGridAdapter extends ListBaseAdapter<NewsTagBean> {
	
	private Activity activity;
	private boolean[] isCheck;
	private HashMap<Integer,String> map;

	public NewsCancelGridAdapter(Activity activity) {
		super(activity);		
		this.activity = activity;
		map = new HashMap<>();

	}

	@Override
	public void appendData(List<NewsTagBean> data, boolean isClearOld) {
		super.appendData(data, isClearOld);
		if (data != null) {
			isCheck = new boolean[data.size()];
			for (int i = 0; i < data.size(); i++) {
				isCheck[i] = false;
			}
		}
	}

	@Override
	public int getCount() {
		
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {

		return position;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final int p = position;
		final NewsTagBean ntb = list.get(position);
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.news_cancel_grid_item, parent,false);
		}
		final Button btn = ViewHolder.get(convertView, R.id.btn_pop_item_id);
		btn.setText(ntb.getTagname());
		LogUtils.i("adapter item hight-->"+ convertView.getMeasuredHeight());
		if (isCheck[position]) {
			btn.setBackgroundResource(R.drawable.pop_item_bg_select);
			btn.setTextColor(activity.getResources().getColor(R.color.color_dd3030));
		} else {
			btn.setBackgroundResource(R.drawable.pop_item_bg_normal);
			btn.setTextColor(activity.getResources().getColor(R.color.color_1a1a1a));

		}
		return convertView;
	}
	@Override
	public View getMyView(int position, View convertView, ViewGroup parent) {		
		return null;
	}

	/**
	 * 改变某一个选项的状态
	 * @param post
	 */
	public void choiceState(int post) {
		/**
		 *  传递过来所点击的position,如果是本身已经是选中状态,就让他变成不是选中状态,
		 *  如果本身不是选中状态,就让他变成选中状态
		 */

		isCheck[post] = isCheck[post] != true;
		if (isCheck[post]) {
			//如果当前position是选中状态的的position也就是当前position是true,就把集合当前position的值存入数据库
			map.put(post,list.get(post).getTagid());
		} else {
			//反之则从数据库里面删除
			map.remove(post);
		}
		this.notifyDataSetChanged();
	}
	public String getTaglist(){
		StringBuilder sb = new StringBuilder();
		for(Integer in : map.keySet()){
			sb.append(map.get(in));
			sb.append(",");
		}
		String result = sb.toString().substring(0,sb.toString().length()-1);

		return result;
	}

}
